package com.bana.fvs.app.module.switches.page;

import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.base.BaseActivity;
import com.bana.fvs.app.base.PageBaseParams;
import com.bana.fvs.app.common.adapter.CommonAdapter;
import com.bana.fvs.app.common.adapter.ViewHolder;
import com.bana.fvs.app.module.switches.entity.SwitchBean;
import com.wei.commonlibrary.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Author : yanftch
 * Date   : 2018/2/21
 * Time   : 16:37
 * Desc   : 雾灯控制&工作模式控制页面
 */

/**
 * 0-雾灯控制
 * 1-工作模式控制
 */
public class SwitchButtonActivity extends BaseActivity {

    private static final String TAG = "dah_SwitchButtonActivity";
    @BindView(R.id.listView)
    ListView mListView;
    @BindView(R.id.btnCommit)
    TextView mBtnCommit;

    private List<SwitchBean> datas;
    private int switchModel = 0;
    private CommonAdapter<SwitchBean> adapter;
    private int choosedPosition = 0;


    public class Params extends PageBaseParams {
        public static final String MODEL = "_switch_model_";
    }

    @Override
    public int setLayout() {
        return R.layout.activity_switch_button;
    }

    @Override
    public void setTitle() {
        switchModel = getIntent().getIntExtra(Params.MODEL, 1);
        String title = switchModel == 1 ? getString(R.string.title_work_model) : getString(R.string.title_wudeng);
        mBaseTitleBarView.setTitleText(title);
        mBaseTitleBarView.setLeftTitleGone();
        mBaseTitleBarView.setLeftDrawable(-1);
    }

    @Override
    public void initWidget() {
        mBtnCommit.setOnClickListener(this);
        datas = new ArrayList<>();
        initData();

        if (null != datas && !datas.isEmpty()) {
            adapter = new CommonAdapter<SwitchBean>(datas, R.layout.item_layout_switch_button_item) {
                @Override
                public void convert(ViewHolder holder, SwitchBean item) {
                    int position = holder.getPosition();

                    if (!TextUtils.isEmpty(item.getSwitchName())) {
                        holder.setText(R.id.tvName, item.getSwitchName());
                    }
                    if (choosedPosition == position) {
                        item.setChoosed(true);
                        holder.setImageResource(R.id.ivChoose, R.drawable.radio_checked);
                    } else {
                        item.setChoosed(false);
                        holder.setImageResource(R.id.ivChoose, R.drawable.radio_unchecked);
                    }

                    if (item.isChoosed()) {
                    } else {
                    }
                }
            };
            mListView.setAdapter(adapter);
        }
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                choosedPosition = position;
                datas.get(position).setChoosed(!datas.get(position).isChoosed());
                adapter.notifyDataSetChanged();
//                Toast.makeText(SwitchButtonActivity.this, "" + datas.get(position).toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 初始化数据
     */
    private void initData() {
        if (switchModel == 0) {//雾灯
            datas.add(new SwitchBean("强制闪黄灯", "1", false, 0));
            datas.add(new SwitchBean("强制闪红灯", "2", false, 0));
            datas.add(new SwitchBean("检测来车红闪", "3", false, 0));
            datas.add(new SwitchBean("关闭", "4", false, 0));
            datas.add(new SwitchBean("重启", "5", true, 0));
        } else if (switchModel == 1) {
            datas.add(new SwitchBean("手动", "01", true, 1));
            datas.add(new SwitchBean("自动", "02", true, 1));
            datas.add(new SwitchBean("定时", "03", true, 1));
        }
    }

    @Override
    public void widgetClick(View v) {
        if (v.getId() == R.id.btnCommit) {
            int size = datas.size();
            SwitchBean switchBean = null;
            for (SwitchBean bean : datas) {
                if (bean.isChoosed()) {
                    switchBean = bean;
                }
            }
            ToastUtil.showShort(mContext, switchBean.toString());
        }
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    public void onTitleLeftPressed() {
        onBackPressed();
    }
}
